<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow @click-left="$router.back()" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ validator: user }]"
        class="importLE"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        class="importLE"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <router-link to="/register" class="regLink"
    >还没有账号，去注册~</router-link
    >
  </div>
</template>

<script>
import { userLogin } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (val) {
      if (this.password && this.username) {
        // let forbidClick = true
        // this.$toast.loading({
        //   message: '登录中...',
        //   forbidClick
        // })
        const { body } = await userLogin(val)
        if (body) {
          // forbidClick = false
          setToken(body.token)
          this.$router.push('/')
        }
        //  else {
        //   forbidClick = false
        // }
      } else {
        this.$toast('用户名或密码不能为空')
      }
    },
    user (val) {
      if (val.length >= 5 && val.length <= 8) {
        if (!/^([a-z]|[A-Z]|[0-9])/.test(val)) {
          console.log(1)
          this.$toast('用户名为5~8位数字或字母')
          return false
        } else {
          console.log(2)
          return true
        }
      } else if (!val) {
        return true
      } else {
        console.log(3)
        this.$toast('用户名为5~8位数字或字母')
        return false
      }
    },
    pass (val) {
      if (val.length > 5 && val.length < 12) {
        if (!/^([a-z]|[A-Z]|[0-9])/.test(val)) {
          this.$toast('密码为5~12位数字或字母')
          return false
        } else {
          return true
        }
      } else if (!val) {
        return true
      } else {
        this.$toast('密码为5~8位数字加字母')
        return false
      }
    }
  }
}
</script>

<style lang="less">
.regLink {
  display: block;
  font-size: 14px;
  text-align: center;
  color: #666;
}
// .van-nav-bar__content {
//   background: @blue;
//   .van-nav-bar__title.van-ellipsis {
//     color: #fff;
//     font-size: 18px;
//   }
//   .van-icon.van-icon-arrow-left.van-nav-bar__arrow {
//     color: #fff;
//   }
// }
.importLE{
  padding: 10px 0 5px 0;
  .van-cell.van-field,input {
    padding: 0 16px;
      height: 60px;
      font-size: 17px;
  }
}
</style>
